package com.dkd.system.service.impl;

import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.system.domain.TbChannel;
import com.dkd.system.domain.TbNode;
import com.dkd.system.domain.TbVmType;
import com.dkd.system.mapper.TbNodeMapper;
import com.dkd.system.mapper.TbVmTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.system.mapper.TbVendingMachineMapper;
import com.dkd.system.domain.TbVendingMachine;
import com.dkd.system.service.ITbVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class TbVendingMachineServiceImpl implements ITbVendingMachineService {
    @Resource
    private TbVendingMachineMapper tbVendingMachineMapper;
    @Resource
    private TbVmTypeMapper tbVmTypeMapper;
    @Autowired
    private TbNodeMapper tbNodeMapper;

    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public TbVendingMachine selectTbVendingMachineById(Long id) {
        return tbVendingMachineMapper.selectTbVendingMachineById(id);
    }

    /**
     * 查询设备列表
     *
     * @param tbVendingMachine 设备
     * @return 设备
     */
    @Override
    public List<TbVendingMachine> selectTbVendingMachineList(TbVendingMachine tbVendingMachine) {
        return tbVendingMachineMapper.selectTbVendingMachineList(tbVendingMachine);
    }
public TbVendingMachine selectVendingMachineByInnerCode(String innerCode){
        return tbVendingMachineMapper.getbyinnercode(innerCode);
}
    /**
     * 新增设备
     *
     * @param tbVendingMachine 设备
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbVendingMachine(TbVendingMachine tbVendingMachine) {
//        System.out.println(tbVendingMachine);
        tbVendingMachine.setInnerCode(UUIDUtils.getUUID());
        TbVmType tbVmType = tbVmTypeMapper.selectTbVmTypeById(tbVendingMachine.getVmTypeId());
        System.out.println(tbVmType);
        tbVendingMachine.setChannelMaxCapacity(tbVmType.getChannelMaxCapacity());
        TbNode tbNode = tbNodeMapper.selectTbNodeById(tbVendingMachine.getNodeId());
        BeanUtils.copyProperties(tbNode, tbVendingMachine, "id");

        tbVendingMachine.setAddr(tbNode.getAddress());

        tbVendingMachine.setCreateTime(DateUtils.getNowDate());
        tbVendingMachine.setVmStatus(0L);
        tbVendingMachine.setUpdateTime(DateUtils.getNowDate());

        int result= tbVendingMachineMapper.insertTbVendingMachine(tbVendingMachine);
        List<TbChannel> list=new ArrayList<>();
        for (int i = 1; i <= 5 ; i++) {
            for (int j = 1; j <= 7; j++) {
                TbChannel tbChannel=new TbChannel();
                tbChannel.setChannelCode(i+"-"+j);
                tbChannel.setVmId(tbVendingMachine.getId());
                tbChannel.setInnerCode(tbVendingMachine.getInnerCode());
                tbChannel.setMaxCapacity(tbVendingMachine.getChannelMaxCapacity());
                tbChannel.setCreateTime(DateTime.now());
                tbChannel.setUpdateTime(DateTime.now());
                list.add(tbChannel);


            }

        }
        tbVendingMachineMapper.batchlist(list);
        return result;
    }

    /**
     * 修改设备
     *
     * @param tbVendingMachine 设备
     * @return 结果
     */
    @Override
    public int updateTbVendingMachine(TbVendingMachine tbVendingMachine) {

        tbVendingMachine.setUpdateTime(DateUtils.getNowDate());
        TbNode tbNode = tbNodeMapper.selectTbNodeById(tbVendingMachine.getNodeId());
//        BeanUtils.copyProperties(tbNode, tbVendingMachine, "id");
        tbVendingMachine.setAddr(tbNode.getAddress());
        tbVendingMachine.setBusinessType(tbNode.getBusinessType());
        tbVendingMachine.setRegionId(tbNode.getRegionId());
        tbVendingMachine.setPartnerId(tbNode.getPartnerId());

        int result= tbVendingMachineMapper.updateTbVendingMachine(tbVendingMachine);

        return result;
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteTbVendingMachineByIds(Long[] ids) {
        return tbVendingMachineMapper.deleteTbVendingMachineByIds(ids);
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteTbVendingMachineById(Long id) {
        return tbVendingMachineMapper.deleteTbVendingMachineById(id);
    }
}
